package org.insia.smartposse.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.insia.smartposse.form.BooksForm;
import org.insia.smartposse.manager.FreelanceBookManager;
import org.insia.smartposse.manager.UserManager;
import org.insia.smartposse.manager.list.FreelanceBooksListManager;

/**
 * @author cchalloy
 *
 */

public class BooksAction extends DispatchAction {

	public ActionForward form(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {

		return mapping.findForward("form");
	}

	public ActionForward addBook(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		BooksForm bookForm = (BooksForm) form;
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		FreelanceBookManager frManager = new FreelanceBookManager();
		frManager.populateBooksForm(user, bookForm);
		frManager.save();

		resp.sendRedirect(req.getContextPath() + "/books.do?cmd=viewBooks");
		return null;
	}

	public ActionForward viewBooks(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp)
	throws Exception {
		req.setAttribute("viewBooks", true);
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		FreelanceBooksListManager listManager = new FreelanceBooksListManager();
		req.setAttribute("books", listManager.findByFreelance(user));

		return mapping.findForward("viewBooks");
	}

	public ActionForward toUpdateBooks(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		BooksForm bookForm = (BooksForm) form;
		Integer idBook = new Integer(req.getParameter("idBook"));
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		FreelanceBookManager frManager = new FreelanceBookManager(idBook);

		if (user.equals(frManager.getFreelance())) {
			bookForm.setId(idBook);
			bookForm.setUrl(frManager.getUrl());
			bookForm.setComment(frManager.getComment());
			return mapping.findForward("toUpdateBooks");
		} else {
			resp.sendRedirect(req.getContextPath() + "/login.do");
			return null;
		}

	}

	public ActionForward updateBooks(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp)
	throws Exception {
		BooksForm bookForm = (BooksForm) form;
		UserManager user = (UserManager) req.getSession().getAttribute("usermanager");
		FreelanceBookManager frManager = new FreelanceBookManager(bookForm.getId());
		frManager.populateBooksForm(user, bookForm);
		frManager.save();

		resp.sendRedirect(req.getContextPath() + "/books.do?cmd=viewBooks");
		return null;
	}

	public ActionForward deleteBook(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse resp)
	throws Exception {
		Integer idBook = new Integer(req.getParameter("idBook"));
		FreelanceBookManager frManager = new FreelanceBookManager(idBook);
		frManager.delete();

		resp.sendRedirect(req.getContextPath() + "/books.do?cmd=viewBooks");
		return null;
	}

}
